package com.example.sqlite_address_list;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    EditText etName;
    EditText etPhone;
    TextView tvShow;
    Button btnAdd,btnDelete,btnUpdate,btnQuary;
    AddressSqlite mySqlite;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    void init(){
        mySqlite = new AddressSqlite(this);
        etName = (EditText)findViewById(R.id.et_name);
        etPhone = (EditText)findViewById(R.id.et_phone);
        tvShow = (TextView)findViewById(R.id.tv_show);
        btnAdd = (Button) findViewById(R.id.btn_add);
        btnDelete = (Button) findViewById(R.id.btn_delete);
        btnUpdate = (Button) findViewById(R.id.btn_update);
        btnQuary = (Button) findViewById(R.id.btn_query);
        btnAdd.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
        btnQuary.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String name,phone;
        SQLiteDatabase db;
        ContentValues values;
        switch (v.getId()){
            case R.id.btn_add: //增 （向表格添加输入框数据）
                name = etName.getText().toString();
                phone = etPhone.getText().toString();
                db = mySqlite.getWritableDatabase();
                values = new ContentValues();
                values.put(AddressSqlite.getColName(),name);
                values.put(AddressSqlite.getColPhone(),phone);
                db.insert(AddressSqlite.getTableName(),null,values);
                Toast.makeText(this,"添加成功！",Toast.LENGTH_SHORT).show();
                db.close();
                break;
            case R.id.btn_delete://删 (删除表格所有记录)
                db = mySqlite.getWritableDatabase();
                db.delete(AddressSqlite.getTableName(),null,null);
                Toast.makeText(this,"删除成功！",Toast.LENGTH_SHORT).show();
                tvShow.setText("");
                db.close();
                break;
            case R.id.btn_update://改 (根据名字修改电话)
                name = etName.getText().toString();
                phone = etPhone.getText().toString();
                db = mySqlite.getWritableDatabase();
                values = new ContentValues();
                values.put(AddressSqlite.getColPhone(),phone);
                db.update(AddressSqlite.getTableName(),values,"name=?",new String[]{name});
                Toast.makeText(this,"修改成功！",Toast.LENGTH_SHORT).show();
                db.close();
                break;
            case R.id.btn_query://查  (查询数据库所有数据)
                db = mySqlite.getReadableDatabase();
                Cursor cursor = db.query(AddressSqlite.getTableName(),null,null,null,null,null,null);
                if(cursor.getCount()==0){
                    tvShow.setText("");
                    Toast.makeText(this,"没有数据！",Toast.LENGTH_SHORT).show();
                }else {
                    cursor.moveToFirst();
                    tvShow.setText("name:"+cursor.getString(1)+", phone:"+cursor.getString(2));
                    while (cursor.moveToNext()){
                        tvShow.append(";\nname:"+cursor.getString(1)+", phone:"+cursor.getString(2));
                    }
                }
                cursor.close();
                db.close();
                break;
        }
    }
}
